package com.zd.controller;

import com.sun.xml.internal.rngom.parse.host.Base;
import com.zd.model.UserInfo;
import com.zd.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/")
public class LoginController extends BaseController {

    @Autowired
    private ILoginService loginService;

    /**
     * 首页登录
     * @return
     */
    @RequestMapping("/")
    public String index(){

        return "login";
    }

    /**
     * 验证用户名与密码是否相等
     * @param userInfo
     * @return
     */
    @ResponseBody
    @PostMapping("login")
    public boolean login(UserInfo userInfo){

        Map<String, Object> resulttMap = loginService.checkLogin(userInfo);
        //登录成功
        if((Boolean) resulttMap.get("result")){

            //获取session
            HttpSession session=getSession();
            session.setAttribute("userInfo",resulttMap.get("userInfo"));



        }


        return (Boolean) resulttMap.get("result");
    }

    /**
     * 退出
     * @return
     */
    @RequestMapping("logout")
    public String logout(){

        //清空session
        HttpSession session = getSession();

        session.removeAttribute("userInfo");

        return "login";
    }




}
